package com.youyun56.gps.android.http;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.youyun56.gps.android.common.CommandFactory;
import com.youyun56.gps.android.service.LocationUploadService;

/**
 * http管理器
 * 
 * @author Administrator
 */
public class HttpManager {
    private final static String TAG = "HttpManager";
    
    public static HttpManager   manager;
    static {
        manager = new HttpManager();
    }
    
    public static HttpManager getInstance() {
        return manager;
    }
    
    public static final String ERROR_CODE = "error_code";
    public static final String ERROR      = "error";
    
    private HttpProvider       provider;
    
    public HttpManager() {
        provider = new HttpProvider();
        
    }
    
    public static final int GET  = 0;
    public static final int POST = 1;
    
    /**
     * 执行http GET请求
     * 
     * @param url
     * @param listener
     */
    public void excuteHttp(String url, IHttpListener listener) {
        if (!hasNetWork())
            return;
        int result = provider.httpGet(url);
        int responseCode = provider.getResponseCode();
        if (result == 1) {
            String strData = provider.getStrResult();
            listener.onBack(true, strData, responseCode);
        } else {
            listener.onBack(false, null, responseCode);
        }
    }
    
    /**
     * 执行http POST请求
     * 
     * @param url
     * @param postParams
     * @param listener
     */
    public void excuteHttp(String url, Map<String, String> postParams, IHttpListener listener) {
        if (!hasNetWork())
            return;
        int result = provider.httpPost(url, postParams);
        int responseCode = provider.getResponseCode();
        if (result == 1) {
            String strData = provider.getStrResult();
            listener.onBack(true, strData, responseCode);
        } else {
            listener.onBack(false, null, responseCode);
        }
    }
    
    /**
     * 单个参数的POST请求
     * 
     * @param url
     * post地址
     * @param param
     * 参数名称
     * @param obj
     * 参数对象
     * @param listener
     */
    public void excutePost(String url, String param, JSONObject obj, IHttpListener listener) {
        if (!hasNetWork())
            return;
        Map<String, String> params = new HashMap<String, String>();
        params.put(param, obj.toString());
        int result = provider.httpPost(url, params);
        int responseCode = provider.getResponseCode();
        if (result == 1) {
            String strData = provider.getStrResult();
            
            listener.onBack(true, strData, responseCode);
        } else {
            listener.onBack(false, null, responseCode);
        }
    }
    
    public void excutePost(String postCommand, IHttpListener listener) {
        if (!hasNetWork())
            return;
        Map<String, String> params = new HashMap<String, String>();
        params.put(CommandFactory.COMMAND, postCommand.toString());
        int result = provider.httpPost(LocationUploadService.url, params);
        int responseCode = provider.getResponseCode();
        if (result == 1) {
            String strData = provider.getStrResult();
            if (listener != null) {
                listener.onBack(true, strData, responseCode);
            }
        } else {
            if (listener != null) {
                listener.onBack(false, null, responseCode);
            }
        }
    }
    
    public void excutePost(String postCommand) {
        excutePost(postCommand, null);
    }
    
    private boolean hasNetWork() {
        // boolean flag = NetWorkUtil.isGprsOpen(null);
        return true;
    }
}
